package ldh.serialize.create;

import java.util.ArrayList;
import java.util.List;

import ldh.serialize.util.ClassInfo;
import ldh.serialize.util.ScanUtil;

public class MakerConfig {

	private List<String> basePackages = new ArrayList<String>();
	
	private List<String> classNames = new ArrayList<String>();
	
	private List<String> excludeClassNames = new ArrayList<String>();
	
	public  MakerConfig basePackage(String basePackage) {
		this.basePackages.add(basePackage);
		return this;
	}
	
	public  MakerConfig className(String className) {
		this.classNames.add(className);
		return this;
	}
	
	public  MakerConfig excludeClassName(String excludeClassName) {
		this.excludeClassNames.add(excludeClassName);
		return this;
	}
	
	public List<ClassInfo> getClassInfoes() {
		List<ClassInfo> cis = new ArrayList<ClassInfo>();
		List<Class<?>> czs = getClasses();
		for (Class<?> cz : czs) {
			ClassInfo ci = new ClassInfo(cz);
			cis.add(ci);
		}
		return cis;
	}
	
	private List<Class<?>> getClasses() {
		List<Class<?>> cs = new ArrayList<Class<?>>();
		for (String bg : this.basePackages) {
			cs.addAll(ScanUtil.getClasses(bg));
		}
		for (String cn : this.classNames) {
			try {
				Class<?> cz = Class.forName(cn);
				cs.add(cz);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		
		List<Class<?>> ed = new ArrayList<Class<?>>();
		for (String t : this.excludeClassNames) {
			for (Class<?> cz : cs) {
				if (cz.getName().endsWith(t)) {
					ed.add(cz);
				}
			}
		}
		
		if (ed.size() > 0) {
			cs.removeAll(ed);
		}
		
		return cs;
	}
}
